<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>User-Agent and Referer Headers [Universal Feed Parser]</title>
<link rel="stylesheet" href="feedparser.css" type="text/css">
<link rev="made" href="mailto:mark@diveintomark.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.65.1">
<meta name="keywords" content="RSS, Atom, CDF, XML, feed, parser, Python">
<link rel="start" href="index.html" title="Documentation">
<link rel="up" href="http.html" title="HTTP Features">
<link rel="prev" href="http-etag.html" title="ETag and Last-Modified Headers">
<link rel="next" href="http-redirect.html" title="HTTP Redirects">
</head>
<body id="feedparser-org" class="docs">
<div class="z" id="intro"><div class="sectionInner"><div class="sectionInner2">
<div class="s" id="pageHeader">
<h1><a href="/"><span>Universal Feed Parser</span></a></h1>
<p><span>Parse RSS and Atom feeds in Python.  3000 unit tests.  Open source.</span></p>
</div>
<div class="s" id="quickSummary"><ul>
<li class="li1">
<a href="http://sourceforge.net/projects/feedparser/"><span>Download</span></a> ·</li>
<li class="li2">
<a href="http://feedparser.org/docs/"><span>Documentation</span></a> ·</li>
<li class="li3">
<a href="http://feedparser.org/tests/"><span>Unit tests</span></a> ·</li>
<li class="li4"><a href="http://sourceforge.net/tracker/?func=browse&amp;group_id=112328&amp;atid=661937"><span>Report a bug</span></a></li>
</ul></div>
</div></div></div>
<div id="main"><div id="mainInner">
<p id="breadcrumb">You are here: <a href="index.html">Documentation</a> → <a href="http.html">HTTP Features</a> → <span class="thispage">User-Agent and Referer Headers</span></p>
<div class="section" lang="en">
<div class="titlepage">
<div>
<div><h2 class="title">
<a name="http.useragent" class="skip" href="#http.useragent" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> User-Agent and Referer Headers</h2></div>
<div><div class="abstract">
<h3 class="title"></h3>
<p><span class="application">Universal Feed Parser</span> sends a default User-Agent string when it requests a feed from a web server.</p>
</div></div>
</div>
<div></div>
</div>
<div class="informalexample">
<p>The default User-Agent string looks like this:</p>
<pre class="programlisting ">UniversalFeedParser/4.1 +http://feedparser.org/</pre>
</div>
<p>If you are embedding <span class="application">Universal Feed Parser</span> in a larger application, you should change the User-Agent to your application name and <acronym title="Uniform Resource Locator">URL</acronym>.</p>
<div class="example">
<a name="example.useragent" class="skip" href="#example.useragent" title="link to this example"><img src="images/permalink.gif" alt="[link]" title="link to this example" width="8" height="9"></a> <h3 class="title">Example: Customizing the User-Agent</h3>
<pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><font color='navy'><b>import</b></font> feedparser</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">d = feedparser.parse('<a href="http://feedparser.org/docs/examples/atom10.xml">http://feedparser.org/docs/examples/atom10.xml</a>', \</span>
<tt class="prompt">... </tt>    <span class="userinput">    agent=<font color='olive'>'MyApp/1.0 +http://example.com/'</font>)</span></pre>
</div>
<p>You can also set the User-Agent once, globally, and then call the <tt class="function">parse</tt> function normally.</p>
<div class="example">
<a name="example.useragent.permanent" class="skip" href="#example.useragent.permanent" title="link to this example"><img src="images/permalink.gif" alt="[link]" title="link to this example" width="8" height="9"></a> <h3 class="title">Example: Customizing the User-Agent permanently</h3>
<pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><font color='navy'><b>import</b></font> feedparser</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">feedparser.USER_AGENT = <font color='olive'>"MyApp/1.0 +http://example.com/"</font></span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">d = feedparser.parse('<a href="http://feedparser.org/docs/examples/atom10.xml">http://feedparser.org/docs/examples/atom10.xml</a>')</span></pre>
</div>
<p><span class="application">Universal Feed Parser</span> also lets you set the referrer when you download a feed from a web server.  This is discouraged, because it is a violation of <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.36">RFC 2616</a>.  The default behavior is to send a blank referrer, and you should never need to override this.</p>
<div class="example">
<a name="example.referrer" class="skip" href="#example.referrer" title="link to this example"><img src="images/permalink.gif" alt="[link]" title="link to this example" width="8" height="9"></a> <h3 class="title">Example: Customizing the referrer</h3>
<pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><font color='navy'><b>import</b></font> feedparser</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">d = feedparser.parse('<a href="http://feedparser.org/docs/examples/atom10.xml">http://feedparser.org/docs/examples/atom10.xml</a>', \</span>
<tt class="prompt">... </tt>    <span class="userinput">referrer=<font color='olive'>'http://example.com/'</font>)</span></pre>
</div>
</div>
<div style="float: left">← <a class="NavigationArrow" href="http-etag.html">ETag and Last-Modified Headers</a>
</div>
<div style="text-align: right">
<a class="NavigationArrow" href="http-redirect.html">HTTP Redirects</a> →</div>
<hr style="clear:both">
<div class="footer"><p class="copyright">Copyright © 2004, 2005, 2006 Mark Pilgrim</p></div>
</div></div>
</body>
</html>
